package io.gitee.chearnee.fw.web.util;

import io.gitee.chearnee.fw.common.model.IResultCode;
import io.gitee.chearnee.fw.common.model.vo.ResultCode;
import io.gitee.chearnee.fw.common.utils.NumberUtil;
import io.gitee.chearnee.fw.web.exception.ApiException;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.util.Assert;

public abstract class FwAsserts extends Assert {
    public static void fail(String message) {
        throw new ApiException(message);
    }

    public static void fail(IResultCode resultCode) {
        fail(resultCode, resultCode.getMessage());
    }

    public static void fail(IResultCode resultCode, String message) {
        throw new ApiException(resultCode, message);
    }
    public static void fail(String messagePattern, Object... objects) {
        FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, objects);
        String message = formattingTuple.getMessage();
        fail(message);
    }

    public static void isPositiveNumber(Number number, String message) {
        if (!NumberUtil.isPositiveNumber(number)) {
            fail(message);
        }
    }

    public static void isNotNull(Object o, String message) {
        if (o == null) {
            fail(message);
        }
    }

    public static void isNotNull(Object o, IResultCode resultCode) {
        if (o == null) {
            fail(resultCode);
        }
    }

    public static void isOk(boolean expression, String message) {
        if (!expression) {
            fail(message);
        }
    }

    public static void verificationFailed() {
        throw new ApiException(ResultCode.VERIFICATION_FAILED);
    }

    public static void noLogin() {
        throw new ApiException(ResultCode.NO_LOGIN);
    }

    public static void noPermission() {
        throw new ApiException(ResultCode.NO_PERMISSION);
    }

    public static void fail(ResultCode resultCode) {
        throw new ApiException(resultCode);
    }
    public static void fail(ResultCode resultCode, String message) {
        throw new ApiException(resultCode,message);
    }

//    public  static class ServiceApi{
//        public static void fail(String message) {
//            throw new InternalServiceApiException(message);
//        }
//    }
}
